﻿using Basic.UserCollect;
using Basic.UserExtendService;
using Basic.UserLocalEvent;
using Basic.UserModel.User;
using Basic.UserRemoteModel.User.Model;
using Basic.UserService.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Basic.UserService.lmpl
{
    internal class UserLogic : IUserLogic
    {
        private readonly IUserCollect _User;
        private readonly IUserConfig _Config;
        private readonly IFileService _File;
        public UserLogic (IUserCollect user, IUserConfig config, IFileService file)
        {
            this._File = file;
            this._User = user;
            this._Config = config;
        }
        public PagingResult<UserData> Query (UserQueryParam query, IBasicPage paging)
        {
            UserData[] list = this._User.Query<UserData>(query, paging, out int count);
            return new PagingResult<UserData>(list, count);
        }
        public void ResetPwd (long userId, string pwd)
        {
            UserPwd user = this._User.Get<UserPwd>(userId);
            pwd = this._Config.CalculatePwd(pwd);
            if (user.Pwd == pwd)
            {
                throw new ErrorException("user.pwd.new.and.old.equal");
            }
            else if (user.UserState != UserRemoteModel.UserState.启用)
            {
                throw new ErrorException("user.already.disable");
            }
            this._SetPwd(userId, pwd);
        }
        private void _SetPwd (long userId, string pwd)
        {
            this._User.SetPwd(userId, pwd);
            new CancelUserBind
            {
                UserId = userId
            }.AsyncPublic();
        }
        public void SetPwd (long userId, string oldPwd, string pwd)
        {
            UserPwd user = this._User.Get<UserPwd>(userId);
            oldPwd = this._Config.CalculatePwd(oldPwd);
            if (user.Pwd != oldPwd)
            {
                throw new ErrorException("user.old.pwd.error");
            }
            else if (user.UserState != UserRemoteModel.UserState.启用)
            {
                throw new ErrorException("user.already.disable");
            }
            pwd = this._Config.CalculatePwd(pwd);
            this._SetPwd(userId, pwd);
        }

        public void Disable (long id)
        {
            UserStateDto user = this._User.Get<UserStateDto>(id);
            if (this._User.Disable(user))
            {
                new CancelUserBind
                {
                    UserId = id
                }.AsyncPublic();
            }
        }
        public void Enable (long id)
        {
            UserStateDto user = this._User.Get<UserStateDto>(id);
            this._User.Enable(user);
        }
        public bool Set (long userId, UserSetDatum datum)
        {
            UserBase user = this._User.Get<UserBase>(userId);
            UserSet set = datum.ConvertMap<UserSetDatum, UserSet>();
            if (this._User.Set(user, set))
            {
                if (set.UserHead != user.UserHead)
                {
                    this._File.Sync(datum.FileId, userId, user.UserHead);
                }
                return true;
            }
            return false;
        }
    }
}
